package p.sunmes.les.actor;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;

import p.sunmes.les.action.CallAction;
import p.sunmes.les.util.U;

public class SwitchButton extends Group {

	Actor on;
	Actor off;
	boolean status;
	CallAction onCall;
	CallAction offCall;

	public SwitchButton(Actor on, Actor off) {
		this.on = on;
		this.off = off;

		addActor(on);
		addActor(off);
		U.disTouch(on);
		U.disTouch(off);

		setSize(on.getWidth(), on.getHeight());
		U.enaTouch(this);

		addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				click();
				return super.touchDown(event, x, y, pointer, button);
			}
		});

		status = false;
		on.setVisible(false);
	}

	public Actor getOn() {
		return on;
	}

	public Actor getOff() {
		return off;
	}

	public boolean getStatus() {
		return status;
	}

	public void setStatus(boolean on) {
		if (on) {
			status = true;
			this.on.setVisible(true);
			off.setVisible(false);
		} else {
			status = false;
			this.on.setVisible(false);
			off.setVisible(true);
		}
	}

	public void click() {
		if (status) {
			status = false;
			on.setVisible(false);
			off.setVisible(true);
			if (offCall != null) {
				offCall.call();
			}
		} else {
			status = true;
			on.setVisible(true);
			off.setVisible(false);
			if (onCall != null) {
				onCall.call();
			}
		}
	}

	public void setOnCall(CallAction call) {
		this.onCall = call;
	}

	public void setOffCall(CallAction call) {
		this.offCall = call;
	}
}
